<%@ include file="/WEB-INF/jsp/includes/include.jsp" %>
<%@ attribute name="postings" required="true" type="java.util.Collection" %>
<h2>List of Your Postings:</h2>
<table cellspacing="0" cellpadding="5" border="1">
	<thead>
		<tr>
			<th>NAME</th>
			<th>RESERVE PRICE</th>
			<th>START DATE</th>
			<th>END DATE</th>
			<th>DURATION</th>
			<th>WINNER</th>
			<th>MAX BID</th>
			<th>STATUS</th>
			<th>EDIT</th>
			<th>DELETE</th>
		</tr>
	</thead>
	<tbody>
	<c:forEach var="posting" items="${postings}" varStatus="rowCounter">
        <c:choose>
          <c:when test="${rowCounter.count % 2 == 0}">
            <c:set var="rowStyle" scope="page" value="row1"/>
          </c:when>
          <c:otherwise>
            <c:set var="rowStyle" scope="page" value="row2"/>
          </c:otherwise>
        </c:choose>
		<tr class="${rowStyle}">
			<td><a href="<c:url value="/postings/show/${posting.id}/"/>"><c:out value="${posting.title}" escapeXml="true" /></a></td>
			<td>$<c:out value="${posting.price}" escapeXml="true" /></td>
			<td>
				<c:choose>
		          <c:when test="${posting.startDate eq null}">
		            not set
		          </c:when>
		          <c:otherwise>
	          		<fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${posting.startDate}" />
		          </c:otherwise>
		        </c:choose>
			</td>
			<td>
				<c:choose>
		          <c:when test="${posting.endDate eq null}">
		            not set
		          </c:when>
		          <c:otherwise>
			          <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${posting.endDate}" />
		          </c:otherwise>
		        </c:choose>
			</td>
			<td>
				<c:choose>
		          <c:when test="${posting.duration eq 0}">
		            4min
		          </c:when>
		          <c:otherwise>
		            ${posting.duration} day(s)
		          </c:otherwise>
		        </c:choose>
			</td>
			<c:choose>
				<c:when test="${fn:length(posting.bids) > 0}">
			    	<c:forEach var="bid" items="${posting.bids}" varStatus="bidCounter">
						<c:if test="${bidCounter.count == 1}">
							<td><span class="error"><strong>${bid.user.firstName} ${bid.user.lastName}</strong></span></td>
							<td><span class="error"><strong>$${bid.price}</strong></span></td>
						</c:if>
					</c:forEach>
				</c:when>
				<c:otherwise>
				  	<td>unknown</td>
				    <td>not placed yet</td>
				</c:otherwise>
			</c:choose>
			<c:if test="${posting.status eq 0}">
				<td><a href="<c:url value="/postings/start/${posting.id}/"/>">Make Active</a></td>
				<td><a href="<c:url value="/postings/edit/${posting.id}/"/>">edit</a></td>
				<td><a href="<c:url value="/postings/delete/${posting.id}/"/>">delete</a></td>
			</c:if>
			<c:if test="${posting.status != 0}">
				<td><c:if test="${posting.status eq 1}">Active</c:if><c:if test="${posting.status eq 2}"><span class="error"><strong>Complete</strong></span></c:if></td>
			</c:if>
			<c:if test="${posting.status != 0}">
				<td>Not available</td>
				<td>Not available</td>
			</c:if>
		</tr>
	</c:forEach>
	</tbody>
</table>